------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  C:/Users/silvio/Documents/CVR/ryp/output/s1_maps.log
  log type:  text
 opened on:  18 Jun 2018, 16:38:33

. clear

. 
. 
. 
. * Step 1: Obtain and install the spmap, shp2dta, and mif2dta commands
. 
. /* If already installed, skip these commmands!
> ssc install spmap
> ssc install shp2dta
> ssc install mif2dta
> */
. 
. /* Step 2: Find a map (an ESRI shapefile or a MapInfo Interchange Format file)
> For Peru these files are obtained from
> http://www.geogpsperu.com/2014/03/base-de-datos-peru-shapefile-shp-minam.html
> */
. 
. *Step 3: Translate the files
. shp2dta using "${coord}/BAS_LIM_DISTRITOS", database("${geocoord}/peru_dist") coordinates("${geocoord}/peru_dist_coor") geni
> d(id_di) 
type: 5

. shp2dta using "${coord}/BAS_LIM_PROVINCIA", database("${geocoord}/peru_prov") coordinates("${geocoord}/peru_prov_coor") geni
> d(id_p) 
type: 5

. shp2dta using "${coord}/BAS_LIM_DEPARTAMENTO", database("${geocoord}/peru_dep") coordinates("${geocoord}/peru_dep_coor") gen
> id(id_de) 
type: 5

. 
. 
. use "${geocoord}/peru_dist_coor", clear

. sort _ID

. by _ID: g x=sum(_X)

. by _ID: replace x=x/_N
(1534582 real changes made)

. by _ID: g y=sum(_Y)

. by _ID: replace y=y/_N
(1534582 real changes made)

. by _ID: keep if _n==_N
(1534582 observations deleted)

. ren _ID id_di

. ren x x_di

. ren y y_di

. sort id_di

. drop _X _Y

. save "${geocoord}/peru_di_acor", replace
(note: file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_di_acor.dta not found)
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_di_acor.dta saved

. 
. 
. use "${geocoord}/peru_prov_coor", clear

. sort _ID

. by _ID: g x=sum(_X)

. by _ID: replace x=x/_N
(662638 real changes made)

. by _ID: g y=sum(_Y)

. by _ID: replace y=y/_N
(662638 real changes made)

. by _ID: keep if _n==_N
(662638 observations deleted)

. ren _ID id_p

. ren x x_p

. ren y y_p

. sort id_p

. drop _X _Y

. save "${geocoord}/peru_p_acor", replace
(note: file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_p_acor.dta not found)
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_p_acor.dta saved

. 
. use "${geocoord}/peru_dep_coor", clear

. sort _ID

. by _ID: g x=sum(_X)

. by _ID: replace x=x/_N
(293745 real changes made)

. by _ID: g y=sum(_Y)

. by _ID: replace y=y/_N
(293745 real changes made)

. by _ID: keep if _n==_N
(293745 observations deleted)

. ren _ID id_de

. ren x x_de

. ren y y_de

. sort id_de

. drop _X _Y

. save "${geocoord}/peru_de_acor", replace
(note: file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_de_acor.dta not found)
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_de_acor.dta saved

. 
. 
. 
. *Step 4: Determine the coding used by the map
. *use "C:/Users/silvio/Documents/CVR/stata_maps/Limite_distrital/peru_dist", clear
. use "${geocoord}/peru_dist", clear

. ren *, lower

. destring iddist,g(lidist)
iddist has all characters numeric; lidist generated as long

. destring idprov,g(liprov)
idprov has all characters numeric; liprov generated as int

. destring iddpto,g(idepa)
iddpto has all characters numeric; idepa generated as byte

. sort liprov

. save, replace
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_dist.dta saved

. 
. use "${geocoord}/peru_prov", clear

. ren *, lower

. destring first_idpr ,g(liprov)
first_idpr has all characters numeric; liprov generated as int

. sort liprov  first_fech

. by liprov: keep if _n==_N
(2 observations deleted)

. sort liprov

. save, replace
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_prov.dta saved

. 
. use "${geocoord}/peru_dep", clear

. ren *, lower

. destring  first_iddp ,g(idepa)
first_iddp has all characters numeric; idepa generated as byte

. sort idepa

. save, replace
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_dep.dta saved

. 
. use "${geocoord}/peru_dist", clear

. merge m:1 liprov using "${geocoord}/peru_prov"

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                             1,834  (_merge==3)
    -----------------------------------------

. drop _merge

. sort idepa

. merge m:1 idepa using "${geocoord}/peru_dep"
count was byte now int

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                             1,834  (_merge==3)
    -----------------------------------------

. drop _merge

. 
. gen iprov=liprov-idepa*100

. gen idist=int(lidist-idepa*10000-iprov*100)

. g loddist=idepa*10000+iprov*100+idist

. 
. do "$code/strata58" /*Creates j that defines 58 strata of BASM*/

. ************************************************
. *We construct now the 58 strata of BASM
. ************************************************
. gen j=.
(1834 missing values generated)

. 
. *1. Loreto, Ucayali, Madre de Dios 
. replace j=1 if idepa==16 
(51 real changes made)

. replace j=1 if idepa==25 
(15 real changes made)

. replace j=1 if idepa==17 
(11 real changes made)

. 
. *2. Callao
. replace j=2 if idepa==7
(6 real changes made)

. 
. *3. Ancash (Bolognesi,Ocros)
. replace j=3 if idepa==2 & iprov==5 
(15 real changes made)

. replace j=3 if idepa==2 & iprov==14 
(10 real changes made)

. 
. *4. Ancash (Huaraz, Huaylas, Mariscal Luzuriaga, Pallasca, Santa, Sihuas, Pomabamba, Recuay, Asuncion, Carhuaz, Aija, Antoni
> o Raymondi, Carlos F. Fitzcarrald, Huari, Huarmey, Casma, Corongo, Yungay)
. *replace j=4 if idepa==2 & j~=3
. replace j=4 if idepa==2 & iprov==1
(12 real changes made)

. replace j=4 if idepa==2 & iprov==2
(5 real changes made)

. replace j=4 if idepa==2 & iprov==3
(6 real changes made)

. replace j=4 if idepa==2 & iprov==4
(2 real changes made)

. replace j=4 if idepa==2 & iprov==6
(11 real changes made)

. replace j=4 if idepa==2 & iprov==7
(3 real changes made)

. replace j=4 if idepa==2 & iprov==8
(4 real changes made)

. replace j=4 if idepa==2 & iprov==9
(7 real changes made)

. replace j=4 if idepa==2 & iprov==10
(16 real changes made)

. replace j=4 if idepa==2 & iprov==11
(5 real changes made)

. replace j=4 if idepa==2 & iprov==12
(10 real changes made)

. replace j=4 if idepa==2 & iprov==13
(8 real changes made)

. replace j=4 if idepa==2 & iprov==15
(11 real changes made)

. replace j=4 if idepa==2 & iprov==16
(4 real changes made)

. replace j=4 if idepa==2 & iprov==17
(10 real changes made)

. replace j=4 if idepa==2 & iprov==18
(9 real changes made)

. replace j=4 if idepa==2 & iprov==19
(10 real changes made)

. replace j=4 if idepa==2 & iprov==20
(8 real changes made)

. 
. *5. Arequipa, Moquegua, La Libertad, Lambayeque, Piura, Ica, Tacna, Tumbes
. replace j=5 if idepa==4 | idepa==18 | idepa==13 | idepa==14 | idepa==18 | idepa==20 | idepa==11 | idepa==23 | idepa==24
(397 real changes made)

. 
. *6. Lima (Oyon, Cajatambo)
. replace j=6 if idepa==15 & iprov==9
(6 real changes made)

. replace j=6 if idepa==15 & iprov==3
(5 real changes made)

. 
. *7. Lima (Huaura, Huarochiri, Huaral, Canta, Barranca)
. replace j=7 if idepa==15 & iprov==8
(12 real changes made)

. replace j=7 if idepa==15 & iprov==7
(32 real changes made)

. replace j=7 if idepa==15 & iprov==6
(12 real changes made)

. replace j=7 if idepa==15 & iprov==4
(7 real changes made)

. replace j=7 if idepa==15 & iprov==2
(5 real changes made)

. 
. * 8. Lima (Lima [Rimac, San Martin de Porres, Villa el Salvador, San Juan de Miraflores])
. replace j=8 if idepa==15 & iprov==1 & idist==28
(1 real change made)

. replace j=8 if idepa==15 & iprov==1 & idist==35
(1 real change made)

. replace j=8 if idepa==15 & iprov==1 & idist==42
(1 real change made)

. replace j=8 if idepa==15 & iprov==1 & idist==33
(1 real change made)

. 
. * 9. Lima (Lima [Carabayllo, Miraflores, Magdalena Vieja, Magdalena del Mar, Jesus Maria, Comas, Independencia, El Agustino,
>  
. *Los Olivos, Lince, La Victoria, La Molina, Puente Piedra, Surquillo, Santiago de Surco, Bre.a, Lima, Ate, Villa Maria del T
> riunfo,
. *Santa Anita, San Juan de Lurigancho, San Isidro, San Borja, San Miguel, San Luis,
. *San Bartolo]
. *replace j=9 if idepa==15 & iprov==1 & j~=8
. replace j=9 if idepa==15 & iprov==1 & idist==1
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==2
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==3
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==4
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==5
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==6
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==7
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==8
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==9
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==10
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==11
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==12
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==13
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==14
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==15
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==16
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==17
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==18
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==19
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==20
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==21
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==22
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==23
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==24
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==25
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==26
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==27
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==29
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==30
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==31
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==32
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==34
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==36
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==37
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==38
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==39
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==40
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==41
(1 real change made)

. replace j=9 if idepa==15 & iprov==1 & idist==43
(1 real change made)

. 
. 
. *10. Lima (Caqete, Yauyos)
. replace j=10 if idepa==15 & iprov==5
(16 real changes made)

. replace j=10 if idepa==15 & iprov==10
(33 real changes made)

. 
. *11. Apurimac, Cusco
. replace j=11 if idepa==3 | idepa==8 
(188 real changes made)

. 
. *12. Amazonas, Cajamarca, San Martin (Rioja, El Dorado, Moyobamba, Lamas, Huallaga)
. replace j=12 if idepa==1
(84 real changes made)

. replace j=12 if idepa==6
(127 real changes made)

. replace j=12 if idepa==22 & iprov==8
(9 real changes made)

. replace j=12 if idepa==22 & iprov==3
(5 real changes made)

. replace j=12 if idepa==22 & iprov==1
(6 real changes made)

. replace j=12 if idepa==22 & iprov==5
(11 real changes made)

. replace j=12 if idepa==22 & iprov==4
(6 real changes made)

. 
. *13. San Martin (Picota, Bellavista)
. replace j=13 if idepa==22 & iprov==7
(10 real changes made)

. replace j=13 if idepa==22 & iprov==2
(6 real changes made)

. 
. *14. Huanuco, San Martin (Mariscal Caceres, Tocache)
. replace j=14 if idepa==10
(76 real changes made)

. replace j=14 if idepa==22 & iprov==6
(5 real changes made)

. replace j=14 if idepa==22 & iprov==10
(5 real changes made)

. 
. *15. Pasco
. replace j=15 if idepa==19
(28 real changes made)

. 
. *16. Ayacucho (Parinacochas, Paucar del Sara Sara)
. replace j=16 if idepa==5 & iprov==7
(8 real changes made)

. replace j=16 if idepa==5 & iprov==8
(10 real changes made)

. 
. 
. *17. Ayacucho (Lucanas [Aucara, Chavipa, Chipao, Laramate, Carmen Salcedo, Cabana])
. replace j=17 if idepa==5 & iprov==6 & idist==2
(1 real change made)

. replace j=17 if idepa==5 & iprov==6 & idist==5
(1 real change made)

. replace j=17 if idepa==5 & iprov==6 & idist==6
(1 real change made)

. replace j=17 if idepa==5 & iprov==6 & idist==8
(1 real change made)

. replace j=17 if idepa==5 & iprov==6 & idist==4
(1 real change made)

. replace j=17 if idepa==5 & iprov==6 & idist==3
(1 real change made)

. 
. * 18. Ayacucho (Lucanas [San Pedro, Puquio, San Pedro de Palco, Santa Ana de Huaycahuacho, Leoncio Prado, San Juan, Lucanas]
> )
. *replace j=18 if idepa==5 & iprov==6 & j~=17
. replace j=18 if idepa==5 & iprov==6 & idist==17
(1 real change made)

. replace j=18 if idepa==5 & iprov==6 & idist==1
(1 real change made)

. replace j=18 if idepa==5 & iprov==6 & idist==18
(1 real change made)

. replace j=18 if idepa==5 & iprov==6 & idist==20
(1 real change made)

. replace j=18 if idepa==5 & iprov==6 & idist==9
(1 real change made)

. replace j=18 if idepa==5 & iprov==6 & idist==16
(1 real change made)

. replace j=18 if idepa==5 & iprov==6 & idist==11
(1 real change made)

. 
. *19. Ayacucho (Huanta [Ayahuanco, Huamanguilla, Llochegua])
. replace j=19 if idepa==5 & iprov==4 & idist==2
(1 real change made)

. replace j=19 if idepa==5 & iprov==4 & idist==3
(1 real change made)

. replace j=19 if idepa==5 & iprov==4 & idist==8
(1 real change made)

.         
. *Ayacucho (Huanta [Huanta])
. replace j=20 if idepa==5 & iprov==4 & idist==1
(1 real change made)

. *Assumption
. *replace j=20 if idepa==5 & iprov==4 & idist==0
.         
. 
. *Ayacucho (Huanta [Iguain])
. replace j=21 if idepa==5 & iprov==4 & idist==4
(1 real change made)

. *Ayacucho (Huanta [Luricocha])
. replace j=22 if idepa==5 & iprov==4 & idist==5
(1 real change made)

. 
. * Ayacucho (Huanta [Santillana])
. replace j=23 if idepa==5 & iprov==4 & idist==6
(1 real change made)

. 
. * Ayacucho (Huanta [Sivia])
. replace j=24 if idepa==5 & iprov==4 & idist==7
(1 real change made)

. 
. * Ayacucho (La Mar [Chungui, Luis Carranza])
. replace j=25 if idepa==5 & iprov==5 & idist==5
(1 real change made)

. replace j=25 if idepa==5 & iprov==5 & idist==6
(1 real change made)

. *Assumption
. *replace j=25 if idepa==5 & iprov==5 & idist==0
. 
. 
. * Ayacucho (La Mar [San Miguel])
. replace j=26 if idepa==5 & iprov==5 & idist==1
(1 real change made)

. 
. * Ayacucho (La Mar [Anco])
. replace j=27 if idepa==5 & iprov==5 & idist==2
(1 real change made)

. 
. * Ayacucho (La Mar [Ayna])
. replace j=28 if idepa==5 & iprov==5 & idist==3
(1 real change made)

. 
. * Ayacucho (La Mar [Chilcas])
. replace j=29 if idepa==5 & iprov==5 & idist==4
(1 real change made)

. 
. * Ayacucho (La Mar [Santa Rosa])
. replace j=30 if idepa==5 & iprov==5 & idist==7
(1 real change made)

. 
. * Ayacucho (La Mar [Tambo])
. replace j=31 if idepa==5 & iprov==5 & idist==8
(1 real change made)

. 
. * Ayacucho (Cangallo)
. replace j=32 if idepa==5 & iprov==2
(6 real changes made)

. 
. * Ayacucho (Huanca Sancos)
. replace j=33 if idepa==5 & iprov==3
(4 real changes made)

. 
. * Ayacucho (Sucre)
. replace j=34 if idepa==5 & iprov==9
(11 real changes made)

. 
. * Ayacucho (Victor Fajardo)
. replace j=35 if idepa==5 & iprov==10
(12 real changes made)

. 
. * Ayacucho (Vilcas Huaman)
. replace j=36 if idepa==5 & iprov==11
(8 real changes made)

. 
. * Ayacucho (Huamanga [Quinua, Ayacucho, Pacaycasa, Jesus Nazareno, Ocros])
. replace j=37 if idepa==5 & iprov==1 & idist==8
(1 real change made)

. replace j=37 if idepa==5 & iprov==1 & idist==1
(1 real change made)

. replace j=37 if idepa==5 & iprov==1 & idist==7
(1 real change made)

. replace j=37 if idepa==5 & iprov==1 & idist==15
(1 real change made)

. replace j=37 if idepa==5 & iprov==1 & idist==6
(1 real change made)

. *Assumption
. *replace j=37 if idepa==5 & iprov==0 & idist==0
. *replace j=37 if idepa==5 & iprov==1 & idist==0
. 
. 
. 
. * Ayacucho (Huamanga [Acos Vinchos])
. replace j=38 if idepa==5 & iprov==1 & idist==3
(1 real change made)

. 
. * Ayacucho (Huamanga [Carmen Alto])
. replace j=39 if idepa==5 & iprov==1 & idist==4
(1 real change made)

. 
. * Ayacucho (Huamanga [Santiago de Pischa, Acocro])
. replace j=40 if idepa==5 & iprov==1 & idist==11
(1 real change made)

. replace j=40 if idepa==5 & iprov==1 & idist==2
(1 real change made)

. 
. * Ayacucho (Huamanga [Chiara])
. replace j=41 if idepa==5 & iprov==1 & idist==5
(1 real change made)

. 
. * Ayacucho (Huamanga [San Jose de Ticllas])
. replace j=42 if idepa==5 & iprov==1 & idist==9
(1 real change made)

. 
. * Ayacucho (Huamanga [San Juan Bautista])
. replace j=43 if idepa==5 & iprov==1 & idist==10
(1 real change made)

. 
. * Ayacucho (Huamanga [Socos])
. replace j=44 if idepa==5 & iprov==1 & idist==12
(1 real change made)

. 
. * Huancavelica (Huancavelica, Tayacaja, Acobamba, Churcampa)
. replace j=45 if idepa==9 & iprov==1 
(19 real changes made)

. replace j=45 if idepa==9 & iprov==7
(16 real changes made)

. replace j=45 if idepa==9 & iprov==2
(8 real changes made)

. replace j=45 if idepa==9 & iprov==5
(10 real changes made)

. 
. * Ayacucho (Huamanga [Vinchos])
. replace j=46 if idepa==5 & iprov==1 & idist==14
(1 real change made)

. 
. 
. *Puno
. replace j=47 if idepa==21
(109 real changes made)

. 
. * Huancavelica (Huaytara, Angaraes)
. replace j=48 if idepa==9 & iprov==6
(16 real changes made)

. replace j=48 if idepa==9 & iprov==3
(12 real changes made)

. 
. * Ayacucho (Huamanga [Tambillo])
. replace j=49 if idepa==5 & iprov==1 & idist==13
(1 real change made)

. 
. * Huancavelica (Castrovirreyna)
. replace j=50 if idepa==9 & iprov==4
(13 real changes made)

.         
. * Junin (Junin, Tarma, Yauli, Chanchamayo)
. replace j=51 if idepa==12 & iprov==5
(4 real changes made)

. replace j=51 if idepa==12 & iprov==7
(9 real changes made)

. replace j=51 if idepa==12 & iprov==8
(10 real changes made)

. replace j=51 if idepa==12 & iprov==3
(6 real changes made)

. 
. 
. * Junin (Jauja, Concepcion, Chupaca)
. replace j=52 if idepa==12 & iprov==4
(34 real changes made)

. replace j=52 if idepa==12 & iprov==2
(15 real changes made)

. replace j=52 if idepa==12 & iprov==9
(9 real changes made)

. 
. * Junin (Huancayo [Huancayo])
. replace j=53 if idepa==12 & iprov==1 & idist==1
(1 real change made)

. 
. * Junin (Huancayo [Chilca])
. replace j=54 if idepa==12 & iprov==1  & idist==7
(1 real change made)

. 
. * Junin (Huancayo [El Tambo])
. replace j=55 if idepa==12 & iprov==1  & idist==14
(1 real change made)

. 
. * Junin (Huancayo [Chupuro, Huayucachi, Hualhuas, Huacrapuquio, Cullhuas,Huasicancha, Huancan, Colca, Sanyo, Santo Domingo d
> e Acobamba, Viques, Sapallanga, Sicaya, Chicche, Chongos Alto, Carhuacallanga, Ingenio, Pucara, Quichuay, Pariahuanca, SanAg
> ustin, San Jeronimo de Tunan, Chacapampa, Pilcomayo, Quilcas])
. *replace j=56 if idepa==12 & iprov==1 & idist~=1 & idist~=7 & idist~=14
. replace j=56 if idepa==12 & iprov==1 & idist==11
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==21
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==17
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==16
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==13
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==20
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==19
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==12
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==32
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==35
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==36
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==33
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==34
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==6
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==8
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==4
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==22
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==26
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==27
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==24
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==29
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==30
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==5
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==25
(1 real change made)

. replace j=56 if idepa==12 & iprov==1 & idist==28
(1 real change made)

. 
. * Junin (Satipo)
. replace j=57 if idepa==12 & iprov==6
(8 real changes made)

. 
. * San Martin (San Martin)
. replace j=58 if idepa==22 & iprov==9
(14 real changes made)

. *Assumption
. *replace j=58 if idepa==22 & iprov==0
. 
. 
. label variable j "Locations"

. label def loca 1 "1 Loreto, Ucayali, Madre de Dios", add

. label def loca 2 "2 Callao", add

. label def loca 3 "3 Ancash (Orcos, Bolognesi)", add

. label def loca 4 "4 Ancash (Rest)", add

. label def loca 5 "5 Arequipa, Moquegua, La Libertad, Lambayeque, Piura, Ica, Tacna, Tumbes", add

. label def loca 6 "6 Lima (Oyon, Cajatambo)", add

. label def loca 7 "7 Lima (Huaura, Huarochiri, Huaral, Canta, Barranca)", add

. label def loca 8 "8 Lima (Lima [Rimac, San Martin de Porres, Villa el Salvador, San Juan de Miraflores])", add

. label def loca 9 "9 Lima (Lima [Rest])", add

. label def loca 10 "10 Lima (Canyete, Yauyos)", add

. label def loca 11 "11 Apurimac, Cusco", add

. label def loca 12 "12 Amazonas, Cajamarca, San Martin (Rioja, El Dorado, Moyobamba, Lamas, Huallaga)", add

. label def loca 13 "13 San Martin (Picota, Bella vista)", add

. label def loca 14 "14 Huanuco, San Martin (Mariscal Caceres, Tocache)", add

. label def loca 15 "15 Pasco", add

. label def loca 16 "16 Ayacucho (Parinacochas, Paucar del Sara Sara)", add

. label def loca 17 "17 Ayacucho (Lucanas [Aucara, Chavipa, Chipao, Laramate, Carmen Salcedo, Cabana])", add

. label def loca 18 "18 Ayacucho (Lucanas [Rest])", add

. label def loca 19 "19 Ayacucho (Huanta [Ayahuanco, Huamanguilla, Llochegua])", add

. label def loca 20 "20 Ayachuco (Huanta [Huanta])", add

. label def loca 21 "21 Ayacucho (Huanta [Iguain])", add

. label def loca 22 "22 Ayacucho (Huanta [Luricocha])", add

. label def loca 23 "23 Ayacucho (Huanta [Santillana])", add

. label def loca 24 "24 Ayacucho (Huanta [Sivia])", add

. label def loca 25 "25 Ayacucho (La Mar [Chungui, Luis Carranza])", add

. label def loca 26 "26 Ayacucho (La Mar [San Miguel])", add

. label def loca 27 "27 Ayacucho (La Mar [Anco])", add

. label def loca 28 "28 Ayacucho (La Mar [Ayna])", add

. label def loca 29 "29 Ayacucho (La Mar [Chilcas])", add

. label def loca 30 "30 Ayacucho (La Mar [Santa Rosa])", add

. label def loca 31 "31 Ayacucho (La Mar [Tambo])", add

. label def loca 32 "32 Ayacucho (Cangallo)", add

. label def loca 33 "33 Ayacucho (Huanca Sancos)", add

. label def loca 34 "34 Ayacucho (Sucre)", add

. label def loca 35 "35 Ayacucho (Victor Fajardo)", add

. label def loca 36 "36 Ayacucho (Vilcas Huaman)", add

. label def loca 37 "37 Ayacucho (Huamanga [Quinua, Ayacucho, Pacaycasa, JNazareno, Orcos])", add

. label def loca 38 "38 Ayacucho (Huamanga [Acos Vinchos])", add

. label def loca 39 "39 Ayacucho (Huamanga [Carmen Alto])", add

. label def loca 40 "40 Ayacucho (Huamanga [Santiago de Pischa, Acocro])", add

. label def loca 41 "41 Ayacucho (Huamanga [Chiara])", add

. label def loca 42 "42 Ayacucho (Huamanga [San Jose de Ticllas])", add

. label def loca 43 "43 Ayacucho (Huamanga [San Juan Bautista])", add

. label def loca 44 "44 Ayacucho (Huamanga [Socos])", add

. label def loca 45 "45 Huancavelica (Huancavelica, Tayacaja, Acobamba, Churcampa)", add

. label def loca 46 "46 Ayacucho (Huamanga [Vinchos])", add

. label def loca 47 "47 Puno", add

. label def loca 48 "48 Huancavelica (Huaytara, Angaraes)", add

. label def loca 49 "49 Ayacucho (Huamanga [Tambillo])", add

. label def loca 50 "50 Huancavelica (Castrovirreyna)", add

. label def loca 51 "51 Junin (Junin, Tarma, Yauli, Chanchamayo)", add

. label def loca 52 "52 Junin (Jauja, Concepcion, Chupaca)", add

. label def loca 53 "53 Junin (Huancayo [Huancayo])", add

. label def loca 54 "54 Junin (Huancayo [Chilca])", add

. label def loca 55 "55 Junin (Huancayo [El Tambo])", add

. label def loca 56 "56 Junin (Huancayo [Rest])", add     

. label def loca 57  "57 Junin (Satipo)", add

. label def loca 58 "58 San Martin (San Martin)", add

. label values j loca

. 
. ************************************************
. * Strata done
. ************************************************
. 
end of do-file

. 
. sort id_di

. merge m:1 id_di using "${geocoord}/peru_di_acor"

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                             1,834  (_merge==3)
    -----------------------------------------

. drop _merge

. 
. sort id_p

. merge m:1 id_p using "${geocoord}/peru_p_acor"

    Result                           # of obs.
    -----------------------------------------
    not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    matched                             1,834  (_merge==3)
    -----------------------------------------

. drop _merge

. 
. sort id_de

. merge m:1 id_de using "${geocoord}/peru_de_acor"

    Result                           # of obs.
    -----------------------------------------
    not matched                             2
        from master                         2  (_merge==1)
        from using                          0  (_merge==2)

    matched                             1,834  (_merge==3)
    -----------------------------------------

. drop _merge

. 
. sort j

. save "${geocoord}/peru_all", replace
(note: file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_all.dta not found)
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/peru_all.dta saved

. 
. 
. 
. /*
> Step 5: Merge datasets
> */
. 
. use "${data}/cvrextrapo", clear

. g loddist=idepa*10000+iprov*100+idist
(687 missing values generated)

. sort j loddist

. by j loddist: g nobs=_N

. by j loddist: keep if _n==_N
(23814 observations deleted)

. sort j nobs

. by j: keep if _n==_N
(1992 observations deleted)

. drop if j==.
(1 observation deleted)

. keep j loddist

. sort j

. merge 1:m j loddist using "${geocoord}/peru_all", keep(3)
(label loca already defined)

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                                58  (_merge==3)
    -----------------------------------------

. keep j x_di y_di 

. ren j i

. sort i

. save "${geocoord}/distxy", replace
(note: file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/distxy.dta not found)
file C:/Users/silvio/Documents/CVR/ryp/data/geocoord/distxy.dta saved

. 
. ****************************************************************************************
. capture log close
